﻿{% extends 'admin/base.html' %}

{%load comm_extras %}

{% block head_soruce %}
<script type="text/javascript" src="/static/common/js/jquery.simplemodal.1.4.min.js"></script> 
<script type="text/javascript" src="/static/admin/js/admin_master.js"></script> 
<script type="text/javascript">
	function checkData(){
		var val = jQuery.trim($("#title").val());
		if(val == ""){
			alert("商品名称不能为空");
			return false;
		}
		val = jQuery.trim($("#price").val());
		if(val == "" || isNaN(val)){
			alert("请输入零售价不能为空");
			return false;
		}
		val = jQuery.trim($("#tuanprice").val());
		if(val == "" || isNaN(val)){
			alert("请输入团购价");
			return false;
		}
		val = jQuery.trim($("#file").val());
		if(val == "" || !isimg(val)){
			alert("请选择图片");
			return false;
		}
		val = jQuery.trim($("#category_ids").val());
		if(val == ""){
			alert("请选择一个商品分类");
			return false;
		}

		return true;
	}

	function selectCategory(){
		var ids = [];
		$("#categoryList :checkbox:checked").each(function(){
			ids.push($(this).val());
		});
		$("#category_ids").val(ids.join(','));
		$("#hid_category_ids").val(ids.join(','));
		$("#categoryList .close").click();
	}

	function initSelectCategory(){
		$("#categoryList :checkbox").attr('checked', false);
		var ids = $("#category_ids").val().split(',');
		for(i in ids){
			$("#liCategory" + ids[i] + " :checkbox").attr('checked', true);
		}
	}

	function selectSupply(){
		var ids = [];
		$("#supplyList :checkbox:checked").each(function(){
			ids.push($(this).val());
		});
		$("#supply_ids").val(ids.join(','));
		$("#hid_supply_ids").val(ids.join(','));
		$("#supplyList .close").click();
	}

	function initSelectSupply(){
		$("#supplyList :checkbox").attr('checked', false);
		var ids = $("#supply_ids").val().split(',');
		for(i in ids){
			$("#liSupply" + ids[i] + " :checkbox").attr('checked', true);
		}
	}
</script>
{% endblock %}

{% block main_content %}

<form id="form1" name="form1" action="{%url admin-product-add-save-json%}"  method="POST" enctype="multipart/form-data">
	<ul id="movie-form" class="form">
	    <li>
            <label for="title">商品名称:</label>
            <input type="text" id="title" name="title" value="{{item.title}}" class="inputText" />
        </li>
		<li>
            <label for="price">零售价:</label>
            <input type="text" id="price" name="price" value="{{item.price}}" class="inputText" />
        </li>
		<li>
            <label for="tuanprice">团购价:</label>
            <input type="text" id="tuanprice" name="tuanprice" value="{{item.tuanprice}}" class="inputText" />
        </li>
		<li>
			<label for="imgFile">图片:</label>
			<input type="file" id="file" name="file"/> <span style="color:red;">请选择一张主图片，其它图片在修改界面添加</span>
		</li>
		<li>
			<label for="category_ids">所属分类：</label>
			<input type="text" id="category_ids" name="category_ids" disabled="disabled" class="inputText" />
			<input type="hidden" id="hid_category_ids" name="hid_category_ids"  />
			<input type="button" value="选择分类" onclick="initSelectCategory();$('#categoryList').modal();" />
		</li>
		<li>
			<label for="supply_ids">所属供应商：</label>
			<input type="text" id="supply_ids" name="supply_ids" disabled="disabled" class="inputText" />
			<input type="hidden" id="hid_supply_ids" name="hid_supply_ids"  />
			<input type="button" value="选择供应商" onclick="initSelectSupply();$('#supplyList').modal();" />
		</li>
		<li>
			<br/>
			<input type="submit" id="btnAdd" value="添加商品" onclick="return checkData();" style="margin-left:500px"/>
		</li>
	</ul>

	<div id="categoryList" class="popBox" style="display:none;width:80%;">
		<a href="javascript:void(0);" class="close simplemodal-close">关闭(X)</a>
		<br class="clearfix" />
	{%if categorys%}
		<input type="button" value="确 定" onclick="selectCategory();" />
		<ul>
		{%for category in categorys %}
			<li>+{{category.title}}</li>
			{%for child in category.subItems %}
				<li id="liCategory{{child.id}}">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{child.title}}
					<input type="checkbox" value="{{child.id}}" />
				</li>
			{%endfor%}
		{%endfor%}
		</ul>
		<input type="button" value="确 定" onclick="selectCategory();" />
		<input type="button" value="取 消" class="simplemodal-close" />
	{%else%}
		<div>还没添加分类</div>
	{%endif%}
	</div>

	<div id="supplyList" class="popBox" style="display:none;width:80%;">
		<a href="javascript:void(0);" class="close simplemodal-close">关闭(X)</a>
		<br class="clearfix" />
	{%if supplys%}
		<input type="button" value="确 定" onclick="selectSupply();" />
		<ul>
		{%for supply in supplys %}
			<li id="liSupply{{supply.id}}">{{supply.title}}
				<input type="checkbox" value="{{supply.id}}" />
			</li>
		{%endfor%}
		</ul>
		<input type="button" value="确 定" onclick="selectSupply();" />
		<input type="button" value="取 消" class="simplemodal-close" />
	{%else%}
		<div>还没添加供应商</div>
	{%endif%}
	</div>

</form>
{% endblock %}
